<template>
  <div>
    <zbTable ref="tableRef" :api-fun="get_list" @action="action" />
    <!-- 新增/编辑弹窗 -->
    <user-edit
      v-if="showEdit"
      v-model:visible="showEdit"
      :actionStatus="actionStatus"
      :editData="editData"
      @done="done"
    />
    <!-- 审核弹窗 -->
    <check-data
      v-if="showCheck"
      v-model:visible="showCheck"
      :editData="editData"
      @done="done"
    />
    <reject v-if="addShow" v-model:visible="addShow" @done="getNewList" />
    <accpets
      v-if="accpetShow"
      v-model:visible="accpetShow"
      :Code="editData"
      @done="receive"
    />
  </div>
</template>

<script setup>
  import { ref, onMounted } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  import { actionFn } from '@/utils/action.js';
  import { daochuFn } from '@/utils/daochu.js';
  import {
    PlusOutlined,
    UploadOutlined,
    ClearOutlined
  } from '@ant-design/icons-vue';
  // import zbTable from '@/components/zbTable/jewelryIndex.vue';
  import zbTable from '@/components/zbTable/receiptsStoreIndex.vue';
  import userEdit from './components/user-edit.vue';
  import checkData from './components/check-data.vue';
  import reject from './components/reject.vue';
  import accpets from './components/accpets.vue';
  import {
    cancel_purchase,
    del_receipts,
    back_verify,
    cancel_verify
  } from '@/api/jewelry/receipts_distribute';
  import { get_list, confirm_receive } from '@/api/jewelry/receipts_enter';
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  const showCheck = ref(false);
  const addShow = ref(false);
  const accpetShow = ref(false);
  // 编辑数据
  const editData = ref({});
  // 编辑按钮数据
  const actionData = ref({});
  // 获取子组件
  const tableRef = ref(null);
  const actionStatus = ref('add');
  const router = useRouter();
  const { push } = useRouter();
  /* 打开编辑弹窗 */
  const action = (data) => {
    editData.value.id = data.row_data.id;
    editData.value.code = data.row_data.code;
    actionData.value = data.action_data;
    if (data.action_data.type == 'edit') {
      showEdit.value = true;
      actionStatus.value = 'edit';
    } else if (data.action_data.type == 'verify') {
      showCheck.value = true;
    } else if (data.action_data.type == 'reject') {
      addShow.value = true;
    } else if (data.action_data.type == 'agree') {
      accpetShow.value = true;
    } else if (data.action_data.type == 'detail') {
      console.log('总部报损', data.row_data);
      toDetail(data.row_data);
    }
  };
  const toDetail = (record) => {
    console.log('看详情');
    let r = router.currentRoute.value.path.split('/');
    let newUrl = `/${r[1]}/${r[2]}/detail`;
    console.log('newUrl', newUrl);
    push({
      path: newUrl,
      query: { code: record.code }
    });
  };
  //接收
  const receive = (val) => {
    console.log('接受', val);
    confirm_receive({
      code: editData.value.code,
      receive_status: 1,
      to_type_code: val
    })
      .then((res) => {
        reloadTable();
        message.success(res.msg);
      })
      .catch((e) => {
        reloadTable();
        message.error(e);
      });
  };
  //拒绝
  const getNewList = (val) => {
    console.log('拒绝', editData.value.code);
    confirm_receive({
      code: editData.value.code,
      receive_status: 2,
      receive_cause: val
    })
      .then((res) => {
        message.success(res.msg);
        reloadTable();
        addShow.value = false;
      })
      .catch((e) => {
        message.error(e);
      });
  };
  const done = (form) => {
    actionFn(actionData.value, form, reloadTable);
  };
  const reloadTable = () => {
    showEdit.value = false;
    showCheck.value = false;
    tableRef.value.reload();
  };
  const daochu = () => {
    let data = {
      url: 'warehouse/receipts_enter/export',
      params: {},
      filename: '总部调拨入库列表'
    };
    daochuFn(data);
  };
</script>
